//机构管理

import r from '@/utils/request';
import type { ListParams, ListResponseType, ResponseType } from '../types';
import { publicKeyAPI } from '../auth';
import { sm2 } from 'sm-crypto';

enum API {
    list = '/company/list', //机构列表
    add = '/company/add',//添加机构
    del = '/company/delete/',//删除机构
    delAll = '/company/deleteAll',//批量删除
    companyget = '/company/get/', //获取单条机构信息
    getCity = '/company/getCity'//获取省市
}
export type GetcompanylistResType = ListResponseType<{
    id: number;
    pid: number;
    name: string;
    address: string;
    telephone: string;
    adminName: string;
}>

export interface companyParam {
    id: number
}
export interface getComponyParams extends ListParams {
    adminName?: string
    name?: string
}

export interface getCompanyAddParams {
    id: number |null;
    name: string;
    province: string;
    city: string;
    address: string;
    telephone: any;
    startTime: any;
    legalPerson: any;
    mobile: any;
    creditCode: any;
    license: number;
    medicalPoint: number;
    house: any;
    certificate: string;
    picture: string;
    adminUserName: string;
    adminPwd: string;
    adminName: string;
    adminMobile: string;
}
export interface CompanyAddRes {
    code: number;
    msg: string;
    data: CompanyAddData;
}

export interface CompanyAddData {
    id: number;
    pid: number;
    name: string;
    address: string;
    province: string;
    city: string;
    telephone: any;
    startTime: any;
    legalPerson: any;
    mobile: any;
    creditCode: any;
    license: number;
    medicalPoint: number;
    house: any;
    certificate: string;
    picture: string;
    addAccountId: number;
    addAccountName: any;
    addTime: string;
    adminId: number;
    adminUserName: string;
    adminPwd: string;
    adminName: string;
    adminMobile: string;
    enable: number;
}
export interface CompanygetRes {
    code: number;
    msg: string;
    data: CompanygetData;
}

export interface CompanygetData {
    id: number;
    pid: number;
    name: string;
    address: string;
    province: string;
    city: string;
    telephone: string;
    startTime: string;
    legalPerson: string;
    mobile: string;
    creditCode: string;
    license: number;
    medicalPoint: number;
    house: string;
    certificate: string;
    picture: string;
    addAccountId: number;
    addAccountName: any;
    addTime: string;
    adminId: number;
    adminUserName: string;
    adminPwd: string;
    adminName: string;
    adminMobile: string;
    enable: number;
}



export interface GetCityRes {
    code: number;
    msg: string;
    data: GetCityData[];
}

export interface GetCityData {
    p: number;
    c?: GetCityData[];
    i: number;
    n: string;
}
export interface BranchEditParam {
    address: string,
    adminMobile: string,
    adminName: string,
    adminPwd: string,
    adminUserName: string,
    city: string,
    id: number,
    name: string,
    province: string,
    telephone: string,
}
export function getCompanylistAPI(params: getComponyParams): Promise<GetcompanylistResType> {
    return r.get(API.list, params);
}
export function AddCompanyAPI(data: getCompanyAddParams): Promise<CompanyAddRes> {
    return r.post(API.add, data);
}
export async function AddCompanyTwoAPI(data: BranchEditParam): Promise<CompanyAddRes> {
    const { data: publicKey } = await publicKeyAPI();
    return r.post(API.add, {
        ...data,
        adminPwd: sm2.doEncrypt(data.adminPwd, publicKey)
    });
    // return r.post(API.add, data);
}
// export function AddCompanyTwoAPI(data: BranchEditParam): Promise<CompanyAddRes> {
//     return r.post(API.add, data);
// }
export function DelCompanyAPI(id: number): Promise<ResponseType<null>> {
    return r.delete(API.del + id);
}
export function DelAllCompanyAPI(id: number[]): Promise<ResponseType<null>> {
    return r.post(API.delAll, id);
}
export function CompanygetAPI(id: number): Promise<CompanygetRes> {
    return r.get(API.companyget + id);
}
export function GetCityAPI(): Promise<GetCityRes> {
    return r.get(API.getCity);
}